package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.BaseContext;
import com.itheima.dao.OrdersMapper;
import com.itheima.pojo.*;
import com.itheima.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * @author Gareth Cao
 * @date 2022/5/2 17:44
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private UserService userService;

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 操作订单表和订单详情表
     *
     * @param orders
     */
    @Override
    @Transactional
    public void submit(Orders orders) {

        //TODO 根据浏览器发来的请求参数,我们要对两张表补全缺失的属性

        //因为订单详情表需要订单表的id属性,订单表需要订单详情表的总计金额属性,互相依赖,所以自己手动生成订单id
        long id = IdWorker.getId();
        //用于统计总共的订单金额
        BigDecimal total = new BigDecimal(0);
        //根据登录用户id查询出用户
        User user = userService.getById(BaseContext.getCurrentId());
        //查出当前用户默认地址
        QueryWrapper<AddressBook> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_default", 1);
        AddressBook addressBook = addressBookService.getOne(queryWrapper);

        //TODO 第一步,补全订单详情表数据
        //订单详情表数据可以通过购物车表来补
        //根据用户id查出购物车里面的信息
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(lambdaQueryWrapper);
        //同一个用户购物车表有多少条数据,订单详情表就应该有多少条数据
        for (ShoppingCart shoppingCart : shoppingCartList) {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setName(shoppingCart.getName());
            orderDetail.setOrderId(id);
            orderDetail.setDishId(shoppingCart.getDishId());
            orderDetail.setSetmealId(shoppingCart.getSetmealId());
            orderDetail.setDishFlavor(shoppingCart.getDishFlavor());
            orderDetail.setNumber(shoppingCart.getNumber());
            //需要设置小计金额,类型为bigDecimal
            orderDetail.setAmount(shoppingCart.getAmount().multiply(new BigDecimal(shoppingCart.getNumber())));
            //将每个订单详情的小计金额累加到total中
            total = total.add(orderDetail.getAmount());
            orderDetail.setImage(shoppingCart.getImage());
            //将orderDetail对象存到订单详情表中
            orderDetailService.save(orderDetail);
        }

        //TODO 第二步,补全订单表数据
        //同一个用户只会有一个订单表
        orders.setId(id);
        //订单号通过uuid设置
        orders.setNumber(UUID.randomUUID().toString());
        orders.setStatus(2);
        orders.setUserId(BaseContext.getCurrentId());
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        //总计的订单金额
        orders.setAmount(total);
        orders.setUserName(user.getName());
        //从默认地址获取收货人的手机,地址,姓名
        orders.setPhone(addressBook.getPhone());
        orders.setAddress(addressBook.getDetail());
        orders.setConsignee(addressBook.getConsignee());
        //存到订单表中
        this.save(orders);

        //TODO 第三步 消除购物车该用户的数据
        shoppingCartService.remove(lambdaQueryWrapper);
    }
}
